﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace LCSFlashCards
{
    [Serializable()]
    public class Deck <T> : IEnumerable<Card<T>>, ISerializable
    {
        public int DeckNumber { get; set; }
        private Queue<Card<T>> cards = new Queue<Card<T>>();
        public event EventHandler<DeckFinishedEventArgs> DeckFinished;

        public IEnumerator<Card<T>> GetEnumerator()
        {
            return cards.GetEnumerator();
        }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return cards.GetEnumerator();
        }
        public int NumberOfCards
        {
            get { return cards.Count; }
        }
        public void AddCard(Card<T> card)
        {
            cards.Enqueue(card);
        }
        internal Card<T> FetchNextCard()
        {
            try
            {
                return cards.Dequeue();
            }
            catch (InvalidOperationException ex)
            {
                if (DeckFinished != null)
                    DeckFinished(this, new DeckFinishedEventArgs());
                return null;
            }
        }
        public Deck()
        {
        }
        public Deck(SerializationInfo info, StreamingContext context)
        {
            this.DeckNumber = (int)info.GetValue("DeckNumber", typeof(int));
            this.cards = (Queue<Card<T>>)info.GetValue("Cards", typeof(Queue<Card<T>>));
            this.DeckFinished = (EventHandler<DeckFinishedEventArgs>)info.GetValue("DeckFinished", typeof(EventHandler<DeckFinishedEventArgs>));
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("DeckNumber", DeckNumber);
            info.AddValue("Cards", cards);
            info.AddValue("DeckFinished", DeckFinished);
        }

    }
}
